﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Runtime.CompilerServices;

namespace FormatConversionTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog.InitialDirectory = desktopPath;
            openFileDialog.Filter = "txt file(*.txt)|*.txt|All file(*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //按行读取数据
                string[] lines = File.ReadAllLines(openFileDialog.FileName, Encoding.UTF8);
                //标识符数组
                string[] BSF = new string[lines.Length - 10];
                NEPoint[] nePoint = new NEPoint[lines.Length - 10];
                //表格行数
                dataGridView1.RowCount = lines.Length - 10;
                //按照分隔符拆分数据
                for (int i = 0; i < lines.Length - 10; i++)
                {
                    string[] temp = lines[i + 10].Split(';');
                    nePoint[i] = new NEPoint(Convert.ToInt32(temp[0]), Convert.ToDouble(temp[1]), Convert.ToDouble(temp[2]), "");
                    BSF[i] = temp[3];
                }
                for (int i = 0; i < nePoint.Length; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = nePoint[i].Xh;
                    dataGridView1.Rows[i].Cells[1].Value = nePoint[i].X;
                    dataGridView1.Rows[i].Cells[2].Value = nePoint[i].Y;
                    dataGridView1.Rows[i].Cells[3].Value = BSF[i];
                }
                //去除重复点
                HashSet<string> exportNE = new HashSet<string>();
                foreach (var item in nePoint)
                {
                    exportNE.Add(item.ToString());
                }
                string nePath = desktopPath + "\\格式转换北京NE.txt";
                if (File.Exists(nePath))
                {
                    MessageBox.Show("是否删除旧数据?", "文件覆盖警告", MessageBoxButtons.OKCancel);
                    File.Delete(desktopPath + "\\格式转换北京NE.txt");
                }
                if (!File.Exists(nePath))
                {
                    foreach (var item in exportNE)
                    {
                        FileAdd(nePath, item + "\n");
                    }
                }
            }
        }
        public static void FileAdd(string path, string strings)
        {
            StreamWriter sw = File.AppendText(path);
            sw.Write(strings);
            sw.Flush();
            //sw.Close();
            sw.Dispose();
        }

        public class NEPoint
        {
            private int _xh;
            private double _x;
            private double _y;
            private string _bsf;
            public int Xh
            {
                get { return _xh; }
                set { _xh = value;}
            }
            public double X
            {
                get { return _x;}
                set {_x = value;}
            }     
            public double Y
            {
                get{ return _y; }
                set{ _y = value;}
            }
            public NEPoint()
            { }
            public string Bsf
            {
                get { return _bsf; }
                set { _bsf = value;}
            }
            public NEPoint(int xh, double x, double y, string bsf)
            {
                _xh = xh;
                _x = x;
                _y = y;
                _bsf = bsf;
            }
            public override string ToString()
            {
                return Xh + "," + Y+ "," + X;
            }
        }
        public class CassPoint
        {
            private string _dm;
            private double _y;
            private double _x;
            public override string ToString()
            {
                return _dm + "," + "," + _x.ToString("f3") + "," + _y.ToString("f3") + ","+ "0";
            }
            public CassPoint(string _dm, double _y, double _x)
            {
                this._dm = _dm;
                this._y = _y;
                this._x = _x;
            }

            public string Dm
            {
                get
                {
                    return _dm;
                }

                set
                {
                    _dm = value;
                }
            }

            public double Y
            {
                get
                {
                    return _y;
                }

                set
                {
                    _y = value;
                }
            }

            public double X
            {
                get
                {
                    return _x;
                }

                set
                {
                    _x =value;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog.InitialDirectory = desktopPath;
            openFileDialog.Filter = "txt file(*.txt)|*.txt|all file(*.*)|*.*";
            if (openFileDialog.ShowDialog()==DialogResult.OK)
            {
                //按行读取数据
                string[] lines = File.ReadAllLines(openFileDialog.FileName, Encoding.Default);
                CassPoint[] cassPoints= new CassPoint[lines.Length];
                for (int i = 0; i < lines.Length; i++)
                {
                    string[] temp = lines[i].Split(',');
                    cassPoints[i] = new CassPoint(temp[0], Convert.ToDouble(temp[1]), Convert.ToDouble(temp[2]));
                }
                string cassPath = desktopPath + "\\格式转换Cass格式.dat";
                if (File.Exists(cassPath))
                {
                    MessageBox.Show("是否删除旧数据", "文件覆盖警告", MessageBoxButtons.OKCancel);
                    File.Delete(cassPath);
                }
               if(!File.Exists(cassPath))
                {
                    foreach (var item in cassPoints)
                    {
                        FileAdd(cassPath, item.ToString()+"\n");
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string desktopPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string path = Path.Combine(desktopPath, "点序列文件.txt");
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            if (!File.Exists(path))
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    FileAdd(path, dataGridView1.Rows[i].Cells[0].Value + "," + dataGridView1.Rows[i].Cells[3].Value+"\n");
                }
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //引导文件位置
            string ydPath = Path.Combine(desktopPath, "点序列文件.txt");
            string cadydPath= Path.Combine(desktopPath, "CAD引导文件.txt");
            if (File.Exists(ydPath))
            {
                string[] ydlines= File.ReadAllLines(ydPath);
                //存储引导信息
                string[] dm=new string[ydlines.Length];
                string[] bsf=new string[ydlines.Length];
                for (int i = 0; i < ydlines.Length; i++)
                {
                    string[] temp = ydlines[i].Split(',');
                    dm[i] = temp[0];
                    bsf[i] = temp[1];
                }
                //选择北京2000文件
                OpenFileDialog openFileDialog= new OpenFileDialog();
                openFileDialog.InitialDirectory = desktopPath;
                openFileDialog.Filter = "txt file(*.txt)|*.txt|all file(*.*)|*.*";
                openFileDialog.Title = "请选择2000坐标文件";
                if (openFileDialog.ShowDialog()==DialogResult.OK)
                {
                    string[] lines = File.ReadAllLines(openFileDialog.FileName);
                    CassPoint[] cassPoints= new CassPoint[lines.Length];
                    for (int i = 0; i < lines.Length; i++)
                    {
                        string[] temp = lines[i].Split(',');
                        cassPoints[i]=new CassPoint(temp[0],Math.Round(Convert.ToDouble(temp[1]),3),Math.Round(Convert.ToDouble(temp[2]),3));
                    }
                    if (File.Exists(cadydPath))
                    {
                        File.Delete(cadydPath);
                    }
                    else if (!File.Exists(cadydPath))
                    {
                        for (int i = 0; i < dm.Length; i++)
                        {
                            FileAdd(cadydPath, dm[i] + ";" + ToX(dm[i],cassPoints) + ";" + ToY(dm[i], cassPoints) + ";" + bsf[i]+"\n");
                        }

                    }
                }

            }
            else if (!File.Exists(ydPath))
            {
                MessageBox.Show("引导文件不存在");
            }
            

        }
        public static double ToY(string xh, CassPoint[] cassPoints)
        {
            for (int i = 0; i < cassPoints.Length; i++)
            {
                if (cassPoints[i].Dm==xh)
                {
                    return Math.Round(cassPoints[i].Y,3);
                }
            }
            return 0;
        }
        public static double ToX(string xh, CassPoint[] cassPoints)
        {
            for (int i = 0; i < cassPoints.Length; i++)
            {
                if (cassPoints[i].Dm == xh)
                {
                    return Math.Round(cassPoints[i].X, 3);
                }
            }
            return 0;
        }



    }
}
